package com.molyfun.parents.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.bean.GetAboutResponse;
import com.molyfun.parents.utils.PreferencesStorageUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:11/23/16.
 */
public class AboutActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.about_content)
    TextView mAboutContent;
//    @InjectView(R.id.webview)
//    WebView mWebView;
    @InjectView(R.id.base_title)
    TextView mBaseTitle;
    @InjectView(R.id.version)
    TextView mVersion;

    private long firstClickTime = 0;
    private int mClickSum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ButterKnife.inject(this);

        getSupportActionBar().setTitle("");
        mBaseTitle.setText("关于我们");

        mBack.setOnClickListener(this);
        mBaseTitle.setOnClickListener(this);

        getAbout();

        mVersion.setText("版本号 V" + MFPApplication.sVersionName);

    }

    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, AboutActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.base_title:
                if (firstClickTime > 0) {
                    long secondClickTime = SystemClock.uptimeMillis();
                    long dtime = secondClickTime - firstClickTime;
                    if (dtime < 2000) {
                        mClickSum++;
                        if (mClickSum >= 8) {
                            toast("开启调试模式", Toast.LENGTH_SHORT);
                            PreferencesStorageUtil.setDebugMode(AboutActivity.this, true);
                        }
                    } else {
                        firstClickTime = SystemClock.uptimeMillis();
                        mClickSum = 1;
                    }
                    return;
                } else {
                    firstClickTime = SystemClock.uptimeMillis();
                    mClickSum = 1;
                }

                break;
            default:
                break;
        }
    }

    private void getAbout() {
        Call<GetAboutResponse> call = MFPApplication.getInstance().getRestClient().getAbout();
        call.enqueue(new Callback<GetAboutResponse>() {
            @Override
            public void onResponse(Call<GetAboutResponse> call, Response<GetAboutResponse> response) {
                if (response != null && response.body() != null && response.body().getData() != null) {
                    if (!TextUtils.isEmpty(response.body().getData().getContent())) {
                        mAboutContent.setText(Html.fromHtml(response.body().getData().getContent()));
                    }
                }
            }

            @Override
            public void onFailure(Call<GetAboutResponse> call, Throwable t) {
            }
        });
    }

}
